').addClass('pbThumbs')
)
);
function prepareDOM(){
noPointerEvents&&overlay.hide();
autoplayBtn.off().on('click', APControl.toggle);
thumbs.off().on('click', 'a', thumbsStripe.click);
isOldIE&&overlay.addClass('msie');
isMobile&&overlay.addClass('mobile');
overlay.off().on('click', 'img', function(e){
e.stopPropagation();
});
$(doc.body).append(overlay);
docElm=doc.documentElement;
}
$.fn.photobox=function(target, settings, callback){
return this.each(function(){
var o, pb,
PB_data=$(this).data('_photobox');
if(PB_data){
if(target==='destroy')
PB_data.destroy();
return this;
}
if(typeof target!='string')
target='a';
if(target==='prepareDOM'){
prepareDOM();
return this;
}
o=$.extend({}, defaults, settings||{});
pb=new Photobox(o, this, target);
$(this).data('_photobox', pb);
pb.callback=callback;
photoboxes.push(pb);
});
}
Photobox=function(_options, object, target){
this.options=$.extend({}, _options);
this.target=target;
this.selector=$(object||doc);
this.thumbsList=null;
var filtered=this.imageLinksFilter(this.selector.find(target));
this.imageLinks=filtered[0];
this.images=filtered[1];
this.init();
};
Photobox.prototype={
init:function(){
var that=this;
if(this.options.thumbs){
this.thumbsList=thumbsStripe.generate.apply(this);
}
this.selector.on('click.photobox', this.target, function(e){
e.preventDefault();
that.open(this);
});
this.observerTimeout=null;
if(this.selector[0].nodeType==1)
that.observeDOM(that.selector[0], function(){
clearTimeout(that.observerTimeout);
that.observerTimeout=setTimeout(function(){
var filtered=that.imageLinksFilter(that.selector.find(that.target)),
activeIndex=0;
if(that.imageLinks.length==filtered[0].length)
return;
that.imageLinks=filtered[0];
that.images=filtered[1];
if(photobox){
if(that.selector==photobox.selector){
images=that.images;
imageLinks=that.imageLinks;
for(var i=images.length; i--;){
if(images[i][0]==activeURL)
return;
}
overlay.removeClass('hasArrows');
}}
if(that.options.thumbs){
that.thumbsList=thumbsStripe.generate.apply(that);
thumbs.html(that.thumbsList);
}
if(that.images.length&&activeURL&&that.options.thumbs){
activeIndex=that.thumbsList.find('a[href="'+activeURL+'"]').eq(0).parent().index();
if(activeIndex==-1)
activeIndex=0;
updateIndexes(activeIndex);
thumbsStripe.changeActive(activeIndex, 0);
}}, 50);
});
},
open:function(link){
var startImage=$.inArray(link, this.imageLinks);
if(startImage==-1)
return false;
options=this.options;
images=this.images;
imageLinks=this.imageLinks;
photobox=this;
this.setup(1);
overlay.on(transitionend, function(){
overlay.off(transitionend).addClass('on');
changeImage(startImage, true);
}).addClass('show');
if(isOldIE)
overlay.trigger('MSTransitionEnd');
return false;
},
imageLinksFilter:function(obj){
var that=this,
images=[],
caption={},
captionlink;
return [obj.filter(function(i){
var link=$(this),
thumbImg;
if(that.options.thumb)
thumbImg=link.find(that.options.thumb)[0];
if(!that.options.thumb||!thumbImg)
thumbImg=link.find('img')[0];
if(thumbImg)
captionlink=thumbImg.getAttribute('data-pb-captionlink');
caption.content=(thumbImg.getAttribute('alt')||thumbImg.getAttribute('title')||'');
if(captionlink){
captionlink=captionlink.split('[');
if(captionlink.length==2){
caption.linkText=captionlink[0];
caption.linkHref=captionlink[1].slice(0,-1);
}else{
caption.linkText=captionlink;
caption.linkHref=captionlink;
}
caption.content +='
' + caption.linkText + '';
}
images.push([link[0].href, caption.content, thumbImg.getAttribute('src')]);
return true;
}), images];
},
observeDOM:(function(){
var MutationObserver=win.MutationObserver||win.WebKitMutationObserver,
eventListenerSupported=win.addEventListener;
return function(obj, callback){
if(MutationObserver){
var obs=new MutationObserver(function(mutations, observer){
if(mutations[0].addedNodes.length||mutations[0].removedNodes.length)
callback();
});
obs.observe(obj, { childList:true, subtree:true });
}
else if(eventListenerSupported){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}}
})(),
setup:function (open){
var fn=open ? "on":"off";
image[0].src=blankImg;
if(options.thumbs){
if(!isMobile){
thumbs[fn]('mouseenter.photobox', thumbsStripe.calc)
[fn]('mousemove.photobox', thumbsStripe.move);
}}
if(open){
image.css({'transition':'0s'}).removeAttr('style');
overlay.show();
thumbs
.html(this.thumbsList)
.trigger('mouseenter.photobox');
if(options.thumbs){
overlay.addClass('thumbs');
}else{
thumbsToggler.prop('checked', false);
overlay.removeClass('thumbs');
}
if(this.images.length < 2||options.single)
overlay.removeClass('thumbs hasArrows hasCounter hasAutoplay');
else{
overlay.addClass('hasArrows hasCounter')
if(options.time > 1000){
overlay.addClass('hasAutoplay');
if(options.autoplay)
APControl.progress.start();
else
APControl.pause();
}
else
overlay.removeClass('hasAutoplay');
}
options.hideFlash&&$('iframe, object, embed').css('visibility', 'hidden');
}else{
$(win).off('resize.photobox');
}
$(doc).off("keydown.photobox")[fn]({ "keydown.photobox": keyDown });
if(isMobile){
overlay.removeClass('hasArrows');
wrapper[fn]('swipe', onSwipe);
}
if(options.zoomable){
overlay[fn]({"mousewheel.photobox": scrollZoom });
if(!isOldIE) thumbs[fn]({"mousewheel.photobox": thumbsResize });
}
if(!options.single){
overlay[fn]({"mousewheel.photobox": wheelNextPrev });
}},
destroy:function(){
options=this.options;
this.selector
.off('click.photobox', this.target)
.removeData('_photobox');
close();
}}
function onSwipe(e, Dx, Dy){
if(Dx==1){
image.css({transform:'translateX(25%)', transition:'.2s', opacity:0});
setTimeout(function(){ changeImage(prevImage) }, 200);
}
else if(Dx==-1){
image.css({transform:'translateX(-25%)', transition:'.2s', opacity:0});
setTimeout(function(){ changeImage(nextImage) }, 200);
}
if(Dy==1)
thumbsToggler.prop('checked', true);
else if(Dy==-1)
thumbsToggler.prop('checked', false);
}
thumbsStripe=(function(){
var containerWidth=0,
scrollWidth=0,
posFromLeft=0,
stripePos=0,
animated=null,
padding,
el, $el, ratio, scrollPos, pos;
return{
generate:function(){
var thumbsList=$('
'),
elements=[],
len=this.imageLinks.size(),
title, thumbSrc, link, type, i;
for(i=0; i < len; i++){
link=this.imageLinks[i];
thumbSrc=this.images[i][2];
if(!thumbSrc)
continue;
title=this.images[i][1];
type=link.rel ? " class='" + link.rel +"'":'';
elements.push(' ');
};
thumbsList.html(elements.join(''));
return thumbsList;
},
click:function(e){
e.preventDefault();
activeThumb.removeClass('active');
activeThumb=$(this).parent().addClass('active');
var imageIndex=$(this.parentNode).index();
return changeImage(imageIndex, 0, 1);
},
changeActiveTimeout:null,
changeActive:function(index, delay, thumbClick){
var lastIndex=activeThumb.index();
activeThumb.removeClass('active');
activeThumb=thumbs.find('li').eq(index).addClass('active');
if(thumbClick||!activeThumb[0]) return;
clearTimeout(this.changeActiveTimeout);
this.changeActiveTimeout=setTimeout(
function(){
var pos=activeThumb[0].offsetLeft + activeThumb[0].clientWidth/2 - docElm.clientWidth/2;
delay ? thumbs.delay(800):thumbs.stop();
thumbs.animate({scrollLeft: pos}, 500, 'swing');
}, 200);
},
calc:function(e){
el=thumbs[0];
containerWidth=el.clientWidth;
scrollWidth=el.scrollWidth;
padding=0.15 * containerWidth;
posFromLeft=thumbs.offset().left;
stripePos=e.pageX - padding - posFromLeft;
pos=stripePos / (containerWidth - padding*2);
scrollPos=(scrollWidth - containerWidth) * pos;
thumbs.animate({scrollLeft:scrollPos}, 200);
clearTimeout(animated);
animated=setTimeout(function(){
animated=null;
}, 200);
return this;
},
move:function(e){
if(animated) return;
ratio=scrollWidth / containerWidth;
stripePos=e.pageX - padding - posFromLeft;
if(stripePos < 0) stripePos=0;
pos=stripePos / (containerWidth - padding*2);
scrollPos=(scrollWidth - containerWidth) * pos;
el.scrollLeft=scrollPos;
}}
})();
APControl={
autoPlayTimer:false,
play:function(){
APControl.autoPlayTimer=setTimeout(function(){ changeImage(nextImage) }, options.time);
APControl.progress.start();
autoplayBtn.removeClass('play');
APControl.setTitle('Click to stop autoplay');
options.autoplay=true;
},
pause:function(){
clearTimeout(APControl.autoPlayTimer);
APControl.progress.reset();
autoplayBtn.addClass('play');
APControl.setTitle('Click to resume autoplay');
options.autoplay=false;
},
progress:{
reset:function(){
autoplayBtn.find('div').removeAttr('style');
setTimeout(function(){ autoplayBtn.removeClass('playing') },200);
},
start:function(){
if(!isOldIE)
autoplayBtn.find('div').css(transition, options.time+'ms');
autoplayBtn.addClass('playing');
}},
setTitle:function(text){
if(text)
autoplayBtn.prop('title', text + ' (every ' + options.time/1000 + ' seconds)');
},
toggle:function(e){
e.stopPropagation();
APControl[ options.autoplay ? 'pause':'play']();
}}
function getPrefixed(prop){
var i, s=doc.createElement('p').style, v=['ms','O','Moz','Webkit'];
if(s[prop]=='') return prop;
prop=prop.charAt(0).toUpperCase() + prop.slice(1);
for(i=v.length; i--;)
if(s[v[i] + prop]=='')
return (v[i] + prop);
}
function keyDown(event){
var code=event.keyCode, ok=options.keys, result;
return ok.close.indexOf(code) >=0&&close() ||
ok.next.indexOf(code) >=0&&!options.single&&loophole(nextImage) ||
ok.prev.indexOf(code) >=0&&!options.single&&loophole(prevImage)||true;
}
function wheelNextPrev(e, dY, dX){
if(dX==1)
loophole(nextImage);
else if(dX==-1)
loophole(prevImage);
}
function next_prev(){
var idx=(this.id=='pbPrevBtn') ? prevImage:nextImage;
loophole(idx);
return false;
}
function updateIndexes(idx){
lastActive=activeImage;
activeImage=idx;
activeURL=images[idx][0];
prevImage=(activeImage||(options.loop ? images.length:0)) - 1;
nextImage=((activeImage + 1) % images.length)||(options.loop ? 0:-1);
}
function loophole(idx){
if(!options.loop){
var afterLast=activeImage==images.length-1&&idx==nextImage,
beforeFirst=activeImage==0&&idx==prevImage;
if(afterLast||beforeFirst)
return;
}
changeImage(idx);
}
function changeImage(imageIndex, firstTime, thumbClick){
if(!imageIndex||imageIndex < 0)
imageIndex=0;
if(!options.loop){
nextBtn[ imageIndex==images.length-1 ? 'addClass':'removeClass' ]('hide');
prevBtn[ imageIndex==0 ? 'addClass':'removeClass' ]('hide');
}
if(typeof options.beforeShow=="function")
options.beforeShow(imageLinks[imageIndex]);
overlay.removeClass('error').addClass(imageIndex > activeImage ? 'next':'prev');
updateIndexes(imageIndex);
stop();
video.empty();
preload.onerror=null;
image.add(video).data('zoom', 1);
activeType=imageLinks[imageIndex].rel=='video' ? 'video':'image';
if(activeType=='video'){
video.html(newVideo()).addClass('hide');
showContent(firstTime);
}else{
var loaderTimeout=setTimeout(function(){ overlay.addClass('pbLoading'); }, 50);
if(isOldIE) overlay.addClass('hide');
options.autoplay&&APControl.progress.reset();
preload=new Image();
preload.onload=function(){
preload.onload=null;
if(prevImage >=0) preloadPrev.src=images[prevImage][0];
if(nextImage >=0) preloadNext.src=images[nextImage][0];
clearTimeout(loaderTimeout);
showContent(firstTime);
};
preload.onerror=imageError;
preload.src=activeURL;
}
captionText.on(transitionend, captionTextChange).addClass('change');
if(firstTime||isOldIE) captionTextChange();
if(options.thumbs)
thumbsStripe.changeActive(imageIndex, firstTime, thumbClick);
history.save();
}
function newVideo(){
var url=images[activeImage][0],
sign=$('').prop('href',images[activeImage][0])[0].search ? '&':'?';
url +=sign + 'vq=hd720&wmode=opaque';
return $("